package com.xique.door.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xique.common.core.text.Convert;
import com.xique.common.core.utils.DateUtils;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.utils.StringUtils;
import com.xique.door.bean.domain.BuildBuilding;
import com.xique.door.bean.domain.BuildBuildingV1;
import com.xique.door.bean.search.build.BuildingListSearch;
import com.xique.door.constant.BuildConstant;
import com.xique.door.mapper.BuildBuildingMapper;
import com.xique.door.mapper.BuildBuildingMapperV1;
import com.xique.door.service.IBuildBuildingService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 楼栋Service业务层处理
 *
 * @author shd
 * @date 2021-09-10
 */
@Service
public class BuildBuildingServiceImpl extends ServiceImpl<BuildBuildingMapperV1, BuildBuildingV1> implements IBuildBuildingService
{
    @Autowired
    private BuildBuildingMapper buildBuildingMapper;

    @Autowired
    private BuildBuildingMapperV1 buildBuildingMapperV1;

    /**
     * 查询楼栋
     *
     * @param id 楼栋ID
     * @return 楼栋
     */
    @Override
    public BuildBuilding selectBuildBuildingById(Long id)
    {
        return buildBuildingMapper.selectBuildBuildingById(id);
    }

    /**
     * 查询楼栋列表
     *
     * @param buildBuilding 楼栋
     * @return 楼栋
     */
    @Override
//    @NeedSetFieldValue
    public List<BuildBuilding> selectBuildBuildingList(BuildBuilding buildBuilding)
    {
        buildBuilding.setDelFlag(BuildConstant.BuildDelFlag.EXIST);
        return buildBuildingMapper.selectBuildBuildingList(buildBuilding);
    }

    @Override
    public List<BuildBuilding> selectBuildBuildingListByAreaIds(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return new ArrayList<>();
        }
        return buildBuildingMapper.selectBuildBuildingListByAreaIds(ids);
    }

    @Override
    public List<BuildBuilding> selectBuildBuildingListByIds(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return new ArrayList<>();
        }
        return buildBuildingMapper.selectBuildBuildingListByIds(ids);
    }

    /**
     * 新增楼栋
     *
     * @param buildBuilding 楼栋
     * @return 结果
     */
    @Override
    public int insertBuildBuilding(BuildBuilding buildBuilding)
    {
        buildBuilding.setCreateTime(DateUtils.getNowDate());
        buildBuilding.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        buildBuilding.setUpdateBy(buildBuilding.getCreateBy());
        buildBuilding.setUpdateTime(DateUtils.getNowDate());
        return buildBuildingMapper.insertBuildBuilding(buildBuilding);
    }

    /**
     * 修改楼栋
     *
     * @param buildBuilding 楼栋
     * @return 结果
     */
    @Override
    public int updateBuildBuilding(BuildBuilding buildBuilding)
    {
        buildBuilding.setUpdateTime(DateUtils.getNowDate());
        buildBuilding.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        return buildBuildingMapper.updateBuildBuilding(buildBuilding);
    }

    /**
     * 删除楼栋对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteBuildBuildingByIds(String ids)
    {
        return buildBuildingMapper.deleteBuildBuildingByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除楼栋信息
     *
     * @param id 楼栋ID
     * @return 结果
     */
    @Override
    public int deleteBuildBuildingById(Long id)
    {
        return buildBuildingMapper.deleteBuildBuildingById(id);
    }

    @Override
    public Map<Long, String> selectBuildingName(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return new HashMap<>(0);
        }

        List<BuildBuilding> list = this.selectBuildBuildingListByIds(ids);
        if (CollectionUtils.isEmpty(list)) {
            return new HashMap<>(0);
        }

        return list.stream().collect(Collectors.toMap(BuildBuilding::getId, BuildBuilding::getBuildingName));
    }

    @Override
    public int batchInsertBuildings(List<BuildBuilding> buildBuildings) {
        for (BuildBuilding buildBuilding : buildBuildings) {
            buildBuilding.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
            buildBuilding.setUpdateBy(buildBuilding.getCreateBy());
            buildBuilding.setCreateTime(new Date());
            buildBuilding.setUpdateTime(buildBuilding.getCreateTime());
        }
        return buildBuildingMapper.batchInsertBuildings(buildBuildings);
    }

    @Override
    public IPage<BuildBuildingV1> selectBuildBuildingPageV1(BuildingListSearch search) {

        QueryWrapper<BuildBuildingV1> condition = new QueryWrapper<>();
        if (StringUtils.isNotNull(search.getParkId())) {
            condition.eq("park_id", search.getParkId());
        }
        if (StringUtils.isNotNull(search.getAreaId())) {
            condition.eq("area_id", search.getAreaId());
        }
        Page<BuildBuildingV1> page = new Page<>();
        if (StringUtils.isNotNull(search.getPageNum()) && StringUtils.isNotNull(search.getPageSize())) {
            page.setCurrent(search.getPageNum());
            page.setSize(search.getPageSize());
        }
        condition.orderByAsc("create_time");
        return buildBuildingMapperV1.selectPage(page, condition);
    }

    @Override
    public List<BuildBuildingV1> selectBuildBuildingListV1(BuildingListSearch search) {

        QueryWrapper<BuildBuildingV1> condition = new QueryWrapper<>();
        if (StringUtils.isNotNull(search.getParkId())) {
            condition.eq("park_id", search.getParkId());
        }
        if (StringUtils.isNotNull(search.getAreaId())) {
            condition.eq("area_id", search.getAreaId());
        }
        if (CollectionUtils.isNotEmpty(search.getIds())) {
            condition.in("id", search.getIds());
        }
        condition.eq("del_flag", "0");
        condition.orderByAsc("create_time");
        return buildBuildingMapperV1.selectList(condition);
    }

    @Override
    public BuildBuildingV1 selectBuildBuildingV1(BuildBuildingV1 buildBuildingV1) {

        QueryWrapper<BuildBuildingV1> condition = new QueryWrapper<>();
        if (StringUtils.isNotNull(buildBuildingV1.getBuildingName())) {
            condition.eq("building_name", buildBuildingV1.getBuildingName());
        }
        if (StringUtils.isNotNull(buildBuildingV1.getAreaId())) {
            condition.eq("area_id", buildBuildingV1.getAreaId());
        }
        if (StringUtils.isNotNull(buildBuildingV1.getParkId())) {
            condition.eq("park_id", buildBuildingV1.getParkId());
        }
        condition.eq("del_flag", "0");
        return buildBuildingMapperV1.selectOne(condition);
    }
}
